package com.hjs.Executors;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class CachedThreadPoolDemo {

    public static void main(String[] args) {
        /**
         * 缓存线程
         */
//        ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
//        for (int i = 0; i < 4; i++) {
//            final int index=i;
//            newCachedThreadPool.execute(new ThreadPoolDemo(index));
//        }
        /**
         * 定长的线程
         *
         */
//        ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(2);
//        for (int i = 0; i < 4; i++) {
//            final int index = i;
//            newFixedThreadPool.execute(new ThreadPoolDemo(index));
//        }
        /**
         * 定长线程支持定时及其周期性任务执行
         */
//        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
//        for (int i = 0; i < 4; i++) {
//            final int index = i;
//            scheduledExecutorService.schedule(new ThreadPoolDemo(2),2, TimeUnit.SECONDS);
//        }
        /**
         * 单利模式进行
         */
        ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 4; i++) {
            final int index = 4 ;
            newSingleThreadExecutor.execute(new ThreadPoolDemo(index));
        }
    }
}
